﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Blog.Models
{
    internal class Context
    {
        internal static readonly Context Instance = new Context();

        List<Post> Posts = new List<Post>();

        /// <summary>
        /// Cria um Post
        /// </summary>
        /// <param name="postToCreate">Post</param>
        public void CreatePost(Post postToCreate)
        {
            postToCreate.PostId = Guid.NewGuid();
            Posts.Add(postToCreate);
        }

        /// <summary>
        /// Atualiza um Post
        /// </summary>
        /// <param name="postId">Id do Post</param>
        /// <param name="newPost">Post</param>
        public void UpdatePost(Guid postId, Post newPost)
        {
            var post = GetPost(postId);
            post.Title = newPost.Title;
            post.PublishedDate = newPost.PublishedDate;
            post.Body = newPost.Body;
        }

        /// <summary>
        /// Remove um Post
        /// </summary>
        /// <param name="postId">Id do Post</param>
        public void DeletePost(Guid postId)
        {
            Posts.Remove((from post in Posts
                          where post.PostId == postId
                          select post).Single());
        }

        /// <summary>
        /// Seleciona um Post
        /// </summary>
        /// <param name="postId">Id do Post</param>
        /// <returns>Post</returns>
        public Post GetPost(Guid postId)
        {
            return (from post in Posts
                    where post.PostId == postId
                    select post).Single();
        }

        /// <summary>
        /// Lista os Posts
        /// </summary>
        /// <returns>Lista de Posts</returns>
        public IEnumerable<Post> GetPosts()
        {
            return Posts;
        }

        /// <summary>
        /// Cria um novo Comentário
        /// </summary>
        /// <param name="postId">Id do Post</param>
        /// <param name="commentToCreate">Comentário</param>
        public void CreateComment(Guid postId, Comment commentToCreate)
        {
            var post = GetPost(postId);
            commentToCreate.CommentId = Guid.NewGuid();
            commentToCreate.PublishedDate = DateTime.Now;
            post.Comments.Add(commentToCreate);
        }
    }
}